package com.kedacom.freedomLabel.model.db;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.TypeConverters;

import com.kedacom.freedomLabel.BuildConfig;
import com.kedacom.freedomLabel.initdata.bean.CategoryNewBean;
import com.kedacom.freedomLabel.initdata.bean.Classes;
import com.kedacom.freedomLabel.initdata.bean.Goods;
import com.kedacom.freedomLabel.initdata.bean.MachineNumber;
import com.kedacom.freedomLabel.initdata.bean.NewProduct;
import com.kedacom.freedomLabel.initdata.bean.Post;
import com.kedacom.freedomLabel.initdata.bean.ProductDetail;
import com.kedacom.freedomLabel.initdata.bean.RubbishBean;
import com.kedacom.freedomLabel.initdata.bean.Staff;
import com.kedacom.freedomLabel.initdata.bean.Team;
import com.kedacom.freedomLabel.initdata.dao.CategoryNewDao;
import com.kedacom.freedomLabel.initdata.dao.ClassesDao;
import com.kedacom.freedomLabel.initdata.dao.GoodsDao;
import com.kedacom.freedomLabel.initdata.dao.MachineNumDao;
import com.kedacom.freedomLabel.initdata.dao.PostDao;
import com.kedacom.freedomLabel.initdata.dao.ProductDetailDao;
import com.kedacom.freedomLabel.initdata.dao.RubbishDao;
import com.kedacom.freedomLabel.initdata.dao.StaffDao;
import com.kedacom.freedomLabel.initdata.dao.TeamDao;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.bean.Category;
import com.kedacom.freedomLabel.model.bean.Config;
import com.kedacom.freedomLabel.model.bean.Item;
import com.kedacom.freedomLabel.model.bean.Operator;
import com.kedacom.freedomLabel.model.bean.Product;
import com.kedacom.freedomLabel.model.bean.Template;
import com.kedacom.freedomLabel.model.db.dao.BusinessDao;
import com.kedacom.freedomLabel.model.db.dao.CategoryDao;
import com.kedacom.freedomLabel.model.db.dao.ConfigDao;
import com.kedacom.freedomLabel.model.db.dao.ItemDao;
import com.kedacom.freedomLabel.model.db.dao.NewProductDao;
import com.kedacom.freedomLabel.model.db.dao.OperatorDao;
import com.kedacom.freedomLabel.model.db.dao.ProductDao;
import com.kedacom.freedomLabel.model.db.dao.TemplateDao;
import com.kedacom.freedomLabel.utils.FileHelp;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.kedacom.lego.util.Utils;

import java.io.File;
import java.io.IOException;

/**
 * @author licheng
 * @since 2019/10/24 18:54
 */
@Database(entities = {
        Business.class,
        Item.class,
        Category.class,
        Product.class,
        Config.class,
        Template.class,
        Operator.class,
        Goods.class,
        ProductDetail.class,
        Classes.class,
        Team.class,
        Staff.class,
        Post.class,
        MachineNumber.class,
        RubbishBean.class,
        CategoryNewBean.class
}, version = 12)
@TypeConverters(Converters.class)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase sAppDatabase;

    public static AppDatabase getInstance() {
        if (sAppDatabase == null) {
            synchronized (AppDatabase.class) {
                if (sAppDatabase == null) {
                    File databasePath = getDatabasePath(BuildConfig.DB_NAME);
                    if (databasePath == null) {
                        databasePath = new File(BuildConfig.DB_NAME);
                    }
                    sAppDatabase = Room.databaseBuilder(Utils.getApp(),
                            AppDatabase.class, databasePath.getAbsolutePath())
                            .allowMainThreadQueries()
                            .fallbackToDestructiveMigration()
                            .build();
                }
            }
        }
        return sAppDatabase;
    }

    public abstract ItemDao itemDao();

    public abstract BusinessDao businessDao();

    public abstract CategoryDao categoryDao();

    public abstract ProductDao productDao();

    public abstract ConfigDao configDao();

    public abstract TemplateDao templateDao();

    public abstract OperatorDao operatorDao();

//    public abstract NewProductDao getNewProductDao();

    public abstract GoodsDao getGoodsDao();
    public abstract ProductDetailDao getProductDetailDao();
    public abstract ClassesDao getClassesDao();
    public abstract TeamDao getTeamDao();
    public abstract StaffDao getStaffDao();
    public abstract PostDao getPostDao();
    public abstract MachineNumDao getMachineNumDao();

    public abstract RubbishDao getRubbishDao();
    public abstract CategoryNewDao getCategoryNewDao();

    /**
     * 获得数据库路径，如果不存在，则创建对象对象
     */
    private static File getDatabasePath(String name) {
        //判断是否存在sd卡
        boolean sdExist = android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState());
        if (!sdExist) {//如果不存在,
            LLog.error("SD卡管理：", "SD卡不存在，请加载SD卡");
            return null;
        } else {//如果存在
            String dbDir = FileHelp.DATABASE_DIR;//数据库所在目录
            String dbPath = FileHelp.DATABASE_DIR + name;//数据库路径
            File dirFile = new File(dbDir);
            if (!dirFile.exists())
                dirFile.mkdirs();
            //数据库文件是否创建成功
            boolean isFileCreateSuccess = false;
            File dbFile = new File(dbPath);
            if (!dbFile.exists()) {
                try {
                    isFileCreateSuccess = dbFile.createNewFile();//创建文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else
                isFileCreateSuccess = true;
            if (isFileCreateSuccess)
                return dbFile;
            else
                return null;
        }
    }

}